#include "systemui.h"
#include "ui_systemui.h"
#include "constants.h"
#include "infoui.h"
#include <QTimer>
#include <QDateTime>

extern int MAJORVER;
extern int MINORVER;
extern int REVIVER;

SystemUI::SystemUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SystemUI)
{
    ui->setupUi(this);
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint );
    //load logo and background
    QPixmap lg(logoStr);
    ui->logo->setPixmap(lg);
    QPixmap bg(backgroundStr);
    QPalette background;
    background.setBrush(QPalette::Background, QBrush(bg));
    setPalette(background);
    //set font color
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    ui->cTime->setPalette(pe);
    ui->lVer->setPalette(pe);
    ui->title->setPalette(pe);
    ui->lyear->setPalette(pe);
    ui->lmonth->setPalette(pe);
    ui->lday->setPalette(pe);
    ui->lhour->setPalette(pe);
    ui->lminute->setPalette(pe);
    //button style
    ui->btnRet->setStyleSheet(blueBtn);
    ui->btnSave->setStyleSheet(greenBtn);
    QPalette btnPe;
    btnPe.setColor(QPalette::ButtonText,Qt::white);
    ui->btnRet->setPalette(btnPe);
    ui->btnSave->setPalette(btnPe);
    //Combo box
    ui->cbYr->setStyleSheet("QScrollBar::vertical {width: 30px;}");
    ui->cbMth->setStyleSheet("QScrollBar::vertical {width: 35px;}");
    ui->cbDy->setStyleSheet("QScrollBar::vertical {width: 35px;}");
    ui->cbHr->setStyleSheet("QScrollBar::vertical {width: 35px;}");
    ui->cbMnt->setStyleSheet("QScrollBar::vertical {width: 35px;}");
    ui->cFlow->setStyleSheet("QCheckBox::indicator {width: 48px; height: 48px;}"
                                "QCheckBox::indicator:unchecked {image: url(C://Users/Evan/Documents/EttaBiotech/EttaF1/img/uncheck.png);}"
                                "QCheckBox::indicator:checked {image: url(C://Users/Evan/Documents/EttaBiotech/EttaF1/img/select.png);}");
    ui->cStatic->setStyleSheet("QCheckBox::indicator {width: 48px; height: 48px;}"
                                "QCheckBox::indicator:unchecked {image: url(C://Users/Evan/Documents/EttaBiotech/EttaF1/img/uncheck.png);}"
                                "QCheckBox::indicator:checked {image: url(C://Users/Evan/Documents/EttaBiotech/EttaF1/img/select.png);}");
//    //Spinbox
//    ui->yr->setStyleSheet("QSpinBox { border: 1px outset grey;padding-right: 25px;}"
//                          "QSpinBox::up-button {subcontrol-position: right; right: 25px; width: 25px; height:38px;}"
//                          "QSpinBox::down-button {subcontrol-position: right; width: 25px; height:38px;}");
//    ui->mth->setStyleSheet("QSpinBox { border: 1px outset grey;padding-right: 25px;}"
//                          "QSpinBox::up-button {subcontrol-position: right; right: 25px; width: 25px; height:38px;}"
//                          "QSpinBox::down-button {subcontrol-position: right; width: 25px; height:38px;}");
//    ui->dy->setStyleSheet("QSpinBox { border: 1px outset grey;padding-right: 25px;}"
//                          "QSpinBox::up-button {subcontrol-position: right; right: 25px; width: 25px; height:38px;}"
//                          "QSpinBox::down-button {subcontrol-position: right; width: 25px; height:38px;}");
//    ui->mnt->setStyleSheet("QSpinBox { border: 1px outset grey;padding-right: 25px;}"
//                          "QSpinBox::up-button {subcontrol-position: right; right: 25px; width: 25px; height:38px;}"
//                          "QSpinBox::down-button {subcontrol-position: right; width: 25px; height:38px;}");
//    ui->sec->setStyleSheet("QSpinBox { border: 1px outset grey;padding-right: 25px;}"
//                          "QSpinBox::up-button {subcontrol-position: right; right: 25px; width: 25px; height:38px;}"
//                          "QSpinBox::down-button {subcontrol-position: right; width: 25px; height:38px;}");
    //Combo Box
    QStringList yearLst;
    for (int i=0; i<100; i++)
    {
        yearLst << QString("%0").arg(i+2015);
    }
    ui->cbYr->addItems(yearLst);
    //month
    QStringList monthLst;
    for (int i=1; i<=12; i++)
    {
        monthLst << QString("%0").arg(i);
    }
    ui->cbMth->addItems(monthLst);
    //day
    QStringList dayLst;
    for (int i=1; i<=31; i++)
    {
        dayLst << QString("%0").arg(i);
    }
    ui->cbDy->addItems(dayLst);
    //hour
    QStringList hourLst;
    for (int i=0; i<=23; i++)
    {
        hourLst << QString("%0").arg(i);
    }
    ui->cbHr->addItems(hourLst);
    //second
    QStringList minuteLst;
    for (int i=0; i<=59; i++)
    {
        minuteLst << QString("%0").arg(i);
    }
    ui->cbMnt->addItems(minuteLst);
    QString qVer = QString("%1 %2.%3.%4").arg(tr("Version: ")).arg(MAJORVER).arg(MINORVER).arg(REVIVER);
    ui->lVer->setText(qVer);

    //Fill in the time
    QDateTime curTime = QDateTime::currentDateTime();
    ui->cbYr->setCurrentText(QString("%1").arg(curTime.date().year()));
    ui->cbMth->setCurrentText(QString("%1").arg(curTime.date().month()));
    ui->cbDy->setCurrentText(QString("%1").arg(curTime.date().day()));
    ui->cbHr->setCurrentText(QString("%1").arg(curTime.time().hour()));
    ui->cbMnt->setCurrentText(QString("%1").arg(curTime.time().minute()));
    QTimer *upTime = new QTimer(this);
    connect(upTime,SIGNAL(timeout()),this,SLOT(updateTime()));
    updateTime();
    upTime->start(1000);
}

SystemUI::~SystemUI()
{
    delete ui;
}

void SystemUI::updateTime()
{
    QDateTime curTime = QDateTime::currentDateTime();
    QString lblTm = curTime.date().toString("yyyy/MM/dd") + "\n " + curTime.time().toString("hh:mm:ss");
    this->ui->cTime->setText(lblTm);
}

void SystemUI::getShow()
{
    this->show();
    this->activateWindow();
    this->raise();
}

void SystemUI::on_btnRet_clicked()
{
    this->hide();
    emit retMain();
}

void SystemUI::on_btnSave_clicked()
{
    //save time
    QString qstrTime = QString("date -s \"%1-%2-%3 %4:%5\"").arg(ui->cbYr->currentText(),
                        ui->cbMth->currentText(), ui->cbDy->currentText(),
                        ui->cbHr->currentText(), ui->cbMnt->currentText());
    system(qstrTime.toStdString().c_str());
    system("hwclock -w -f /dev/rtc1");
    InfoUI ifu;
    ifu.InfoDialog();
    ifu.SetInfo(tr("Info"), tr("Time set!"));
    ifu.exec();
}
